// 本地存储管理
const STORAGE_KEYS = {
  USER_INFO: 'userInfo',
  FAVORITES: 'favorites',
  PLAY_HISTORY: 'playHistory',
  CURRENT_PLAYING: 'currentPlaying'
};

// 保存用户信息
function saveUserInfo(userInfo) {
  try {
    wx.setStorageSync(STORAGE_KEYS.USER_INFO, userInfo);
    return true;
  } catch (e) {
    console.error('保存用户信息失败', e);
    return false;
  }
}

// 获取用户信息
function getUserInfo() {
  try {
    return wx.getStorageSync(STORAGE_KEYS.USER_INFO) || null;
  } catch (e) {
    console.error('获取用户信息失败', e);
    return null;
  }
}

// 清除用户信息
function clearUserInfo() {
  try {
    wx.removeStorageSync(STORAGE_KEYS.USER_INFO);
    return true;
  } catch (e) {
    console.error('清除用户信息失败', e);
    return false;
  }
}

// 添加收藏
function addFavorite(music) {
  try {
    let favorites = wx.getStorageSync(STORAGE_KEYS.FAVORITES) || [];
    // 检查是否已收藏
    const exists = favorites.some(item => item.id === music.id);
    if (!exists) {
      favorites.unshift({
        ...music,
        favoriteTime: Date.now()
      });
      wx.setStorageSync(STORAGE_KEYS.FAVORITES, favorites);
    }
    return true;
  } catch (e) {
    console.error('添加收藏失败', e);
    return false;
  }
}

// 移除收藏
function removeFavorite(musicId) {
  try {
    let favorites = wx.getStorageSync(STORAGE_KEYS.FAVORITES) || [];
    favorites = favorites.filter(item => item.id !== musicId);
    wx.setStorageSync(STORAGE_KEYS.FAVORITES, favorites);
    return true;
  } catch (e) {
    console.error('移除收藏失败', e);
    return false;
  }
}

// 获取收藏列表
function getFavorites() {
  try {
    return wx.getStorageSync(STORAGE_KEYS.FAVORITES) || [];
  } catch (e) {
    console.error('获取收藏列表失败', e);
    return [];
  }
}

// 检查是否已收藏
function isFavorite(musicId) {
  try {
    const favorites = wx.getStorageSync(STORAGE_KEYS.FAVORITES) || [];
    return favorites.some(item => item.id === musicId);
  } catch (e) {
    console.error('检查收藏状态失败', e);
    return false;
  }
}

// 添加播放历史
function addPlayHistory(music) {
  try {
    let history = wx.getStorageSync(STORAGE_KEYS.PLAY_HISTORY) || [];
    // 移除已存在的记录
    history = history.filter(item => item.id !== music.id);
    // 添加到开头
    history.unshift({
      ...music,
      playTime: Date.now()
    });
    // 只保留最近50条
    if (history.length > 50) {
      history = history.slice(0, 50);
    }
    wx.setStorageSync(STORAGE_KEYS.PLAY_HISTORY, history);
    return true;
  } catch (e) {
    console.error('添加播放历史失败', e);
    return false;
  }
}

// 获取播放历史
function getPlayHistory() {
  try {
    return wx.getStorageSync(STORAGE_KEYS.PLAY_HISTORY) || [];
  } catch (e) {
    console.error('获取播放历史失败', e);
    return [];
  }
}

// 清除播放历史
function clearPlayHistory() {
  try {
    wx.removeStorageSync(STORAGE_KEYS.PLAY_HISTORY);
    return true;
  } catch (e) {
    console.error('清除播放历史失败', e);
    return false;
  }
}

// 保存当前播放状态
function saveCurrentPlaying(playInfo) {
  try {
    wx.setStorageSync(STORAGE_KEYS.CURRENT_PLAYING, playInfo);
    return true;
  } catch (e) {
    console.error('保存播放状态失败', e);
    return false;
  }
}

// 获取当前播放状态
function getCurrentPlaying() {
  try {
    return wx.getStorageSync(STORAGE_KEYS.CURRENT_PLAYING) || null;
  } catch (e) {
    console.error('获取播放状态失败', e);
    return null;
  }
}

module.exports = {
  saveUserInfo,
  getUserInfo,
  clearUserInfo,
  addFavorite,
  removeFavorite,
  getFavorites,
  isFavorite,
  addPlayHistory,
  getPlayHistory,
  clearPlayHistory,
  saveCurrentPlaying,
  getCurrentPlaying
};

